package com.syqc.gpssdk

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ListView
import android.widget.TextView
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.syqc.monitor.ui.simplemonitor.SimpleMonitorActivity
import com.syqc.sensor.demp.TempDampChartActivity
import com.syqc.monitor.ui.singlemap.SingleMapActivity
import com.syqc.login.ILoginCallback
import com.syqc.login.Init
import com.syqc.login.LoginUtil
import com.syqc.login.MD5Utils
import com.syqc.monitor.ui.carmonitor.CarMonitorActivity
import com.syqc.monitor.ui.carsmonitor.MultiVehicleActivity
import com.syqc.monitor.ui.historyline.HistoryLineActivity
import com.syqc.monitor.ui.list.ListMonitorActivity
import com.syqc.monitor.ui.mile.MileActivity
import com.syqc.monitor.ui.newaddcar.AddCarActivity
import com.syqc.monitor.ui.photo.PhotosActivity
import com.syqc.monitor.ui.realalarm.RealAlarmActivity
import com.syqc.monitor.ui.stopcar.StopCarActivity
import com.syqc.sensor.demp.DampActivity
import com.syqc.sensor.oil.ReportOilActivity
import com.syqc.sensor.recent.RecentOilActivity
import com.syqc.sensor.tdemp.ReportTempActivity
import com.syqc.sensor.water.ReportWaterActivity
import com.syqc.sensor.weight.ReportWeightActivity
import com.syqc.videos.VideotapeActivity
import com.syqc.videos.player.RealPlayerActivity

class MainActivity : AppCompatActivity() {
    private val info = ArrayList<DemoInfo>()
    private var isLogin = false
    private var errorMsg = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        Init.initUtil(application)
        initAct()
        val adatper = DemoListAdapter(info)
        val listView = findViewById<ListView>(R.id.listView)
        listView.adapter = adatper

        LoginUtil.login(
            "ys.31gps.net", "9999", "yfzy", MD5Utils.getmd5("123456"),
            object : ILoginCallback {
                override fun onSuccess() {
                    isLogin = true
                }

                override fun onFailure(errorMsg: String) {
                    isLogin = false
                    this@MainActivity.errorMsg = errorMsg
                    Toast.makeText(this@MainActivity, errorMsg, Toast.LENGTH_SHORT).show()
                }

            },isHttps = false)
        listView.setOnItemClickListener { parent, view, position, id ->
            if (!isLogin) {
                Toast.makeText(this@MainActivity, errorMsg, Toast.LENGTH_SHORT).show()
                return@setOnItemClickListener
            }
            startActivity(Intent(this@MainActivity, info[position].clazz))
        }
    }


    private fun initAct() {

        info.add(DemoInfo("单车监控", CarMonitorActivity::class.java))
        info.add(DemoInfo("多车监控", MultiVehicleActivity::class.java))
        info.add(DemoInfo("列表监控", ListMonitorActivity::class.java))
        info.add(DemoInfo("极简监控", SimpleMonitorActivity::class.java))
        info.add(DemoInfo("历史轨迹", HistoryLineActivity::class.java))
        info.add(DemoInfo("实时报警", RealAlarmActivity::class.java))
        info.add(DemoInfo("实时视频", RealPlayerActivity::class.java))
        info.add(DemoInfo("录像回放", VideotapeActivity::class.java))
        info.add(DemoInfo("单车跟踪", SingleMapActivity::class.java))
        info.add(DemoInfo("里程统计", MileActivity::class.java))
        info.add(DemoInfo("照片查询", PhotosActivity::class.java))
        info.add(DemoInfo("停车统计", StopCarActivity::class.java))

        info.add(DemoInfo("油量统计", ReportOilActivity::class.java))
        info.add(DemoInfo("水位统计", ReportWaterActivity::class.java))
        info.add(DemoInfo("温度统计", ReportTempActivity::class.java))
        info.add(DemoInfo("湿度统计", DampActivity::class.java))
        info.add(DemoInfo("载重统计", ReportWeightActivity::class.java))
        info.add(DemoInfo("近期油耗", RecentOilActivity::class.java))

        info.add(DemoInfo("新增车辆", AddCarActivity::class.java))
    }

    inner class DemoListAdapter(private val data: ArrayList<DemoInfo>) : BaseAdapter() {
        override fun getView(index: Int, convertView: View?, parent: ViewGroup): View {
            var view = convertView
            if (null == view) {
                view = View.inflate(this@MainActivity, R.layout.demo_list_item, null)
            }
            val title = view!!.findViewById<TextView>(R.id.tvTitle)
            title.text = data[index].title
            return view
        }

        override fun getCount(): Int {
            return data.size
        }

        override fun getItem(index: Int): Any {
            return data[index]
        }

        override fun getItemId(id: Int): Long {
            return id.toLong()
        }
    }
}